//
// freeclk.c	free-running clock module 
//		freeclk_init() initializes the module
//		uint32 freeclk_read() reads the clock value in milliseconds
//		
//		(c) Copyright 2010 Communications Programming, Inc.

#include "userLib.h"

// initialize free-running clock

uint32 freeclkValue;			// free-running clock
void freeclk_init(void)
{
	freeclkValue = 0;			// reset timer value
	T0CON = 0xC5;				// timer on, prescale 1:64
	TMR0H = 0;
	TMR0L = freeclk_timer_reset_value;
	INTCONbits.TMR0IF = 0;		// reset interrupt flag
	INTCONbits.TMR0IE = 1;		// enable interrupt
}	

uint32 freeclk_read(void)			// read free-running clock
{
	return(freeclkValue);
}
	
// freeclock interrupt service routine

void freeclk_interrupt(void)
{
	if(INTCONbits.TMR0IF)	// TMR0 interrupt occurred
	{
		++freeclkValue;			// increment clock value
		TMR0L = freeclk_timer_reset_value;
		INTCONbits.TMR0IF = 0;	// reset flag
	}	
}	
	
